<template>
  <div>
    <hb-form v-bind="searchFormConfig" v-model="formData">
      <template #header>
        <h2>高级检索</h2>
      </template>
      <template #footer>
        <div class="handle-footer">
          <el-button @click="handleRefreshClick">
            <el-icon style="vertical-align: middle"><refresh /></el-icon>
            <span>重置</span>
          </el-button>
          <el-button type="primary" @click="handleQueryClick">
            <el-icon style="vertical-align: middle">
              <search />
            </el-icon>
            <span>搜索</span>
          </el-button>
        </div>
      </template>
    </hb-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import HbForm from '@/base-ui/form'

export default defineComponent({
  props: {
    searchFormConfig: {
      type: Object,
      required: true
    }
  },
  components: {
    HbForm
  },
  emits: ['handleReset', 'handleQuery'],
  setup(props, { emit }) {
    const formItems = props.searchFormConfig?.formItems ?? []
    const formOriginData: any = {}

    for (const item of formItems) {
      formOriginData[item.field] = ''
    }

    const formData = ref(formOriginData)

    const handleRefreshClick = () => {
      //双向绑定法：---> form_model.vue
      // for (const key in formOriginData) {
      //   formData.value[`${key}`] = formOriginData[key]
      // }

      formData.value = formOriginData
      emit('handleReset')
    }

    const handleQueryClick = () => {
      emit('handleQuery', formData.value)
    }

    return {
      formData,
      handleRefreshClick,
      handleQueryClick
    }
  }
})
</script>

<style scoped>
.handle-footer {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
